//
//  DouBanViewController.m
//  RACExample
//
//  Created by 山刘 on 2020/6/2.
//  Copyright © 2020 山刘. All rights reserved.
//

#import "DouBanViewController.h"
#import "DouBanViewDetailController.h"

#pragma mark - ====================M层====================
#pragma mark - ====================VM层====================
@interface DouBanViewModel : NSObject
@property (nonatomic, copy) NSString *searchConditons;
@property (nonatomic, strong, readonly) RACSignal  *searchBtnEnableSignal;
@end
@implementation DouBanViewModel
#pragma mark - init
-(instancetype)init{
    if (self = [super init]) {
        [self racInit];
    }
    return self;
}
#pragma mark - business
- (void)racInit {
    _searchBtnEnableSignal = [RACSignal combineLatest:@[RACObserve(self, searchConditons)] reduce:^id(NSString *searchConditions){
        return @(searchConditions.length);
    }];
}
#pragma mark - getter
@end
#pragma mark - ====================V层====================


@interface DouBanViewController ()
@property (weak, nonatomic) IBOutlet UITextField *inputTextField;
@property (weak, nonatomic) IBOutlet UIButton *searchButton;

@property(nonatomic, strong) DouBanViewModel *viewModel;
@end

@implementation DouBanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [super viewDidLoad];
    [self segInitViews];
    [self bindViewModel];
}

#pragma mark - init Views
-(void)segInitViews{
    self.searchButton.enabled = false;
}

#pragma mark - RAC Data Binding
- (void)bindViewModel {
    RAC(self.viewModel,searchConditons) = self.inputTextField.rac_textSignal;
    RAC(self.searchButton,enabled) = self.viewModel.searchBtnEnableSignal;
    @weakify(self);
    [[self.searchButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self.inputTextField resignFirstResponder];
        
        DouBanViewDetailController *vc = [[DouBanViewDetailController alloc] init];
        vc.view.backgroundColor = [UIColor whiteColor];
        vc.title = @"详情";
        vc.conditions = self.inputTextField.text;
        [self.navigationController pushViewController:vc animated:YES];
    }];
}

#pragma mark - getter
-(DouBanViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[DouBanViewModel alloc]init];
    }
    return _viewModel;
}
@end
